package cn.com.qingz.modules.user.service.impl;

import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.mail.MessagingException;
import javax.servlet.http.HttpServletRequest;

import org.apache.shiro.crypto.hash.Sha512Hash;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cn.com.qingz.core.constantes.SystemConstantes;
import cn.com.qingz.core.exception.DicmfException;
import cn.com.qingz.core.utils.MD5;
import cn.com.qingz.core.utils.SendMail;
import cn.com.qingz.modules.system.service.impl.GeneralServiceImpl;
import cn.com.qingz.modules.user.mapper.CustomerMapper;
import cn.com.qingz.modules.user.mapper.DesignCaseMapper;
import cn.com.qingz.modules.user.mapper.OrganizationMapper;
import cn.com.qingz.modules.user.model.Customer;
import cn.com.qingz.modules.user.model.Design;
import cn.com.qingz.modules.user.model.DesignCase;
import cn.com.qingz.modules.user.service.CustomerService;
import cn.com.qingz.modules.user.vo.CustomerSearchCondition;

import com.github.miemiedev.mybatis.paginator.domain.PageBounds;
import com.github.miemiedev.mybatis.paginator.domain.PageList;

@Service
public class CustomerServiceImpl extends GeneralServiceImpl<Customer> implements CustomerService {

    @Autowired
    private CustomerMapper customerMapper;

    @Autowired
    private OrganizationMapper mapper;

    @Autowired
    private DesignCaseMapper designCaseMapper;

    public PageList<Customer> queryForPage(Object condition, int page, int limit) {
        PageList<Customer> list = customerMapper.queryForPage((CustomerSearchCondition) condition, new PageBounds(page, limit));
        return list;
    }

    public void doEx(Integer customerid) {
        Customer c = customerMapper.selectByPrimaryKey(customerid);
        c.setStatus("3");
        c.setUsertype("2");
        customerMapper.updateByPrimaryKeySelective(c);

        try {
            SendMail.sendMessage("smtp.163.com", "hulongcoder@163.com",
                    "codeblue1989", c.getEmail(), "恭喜你获得唻配设计师认证",
                    "尊敬的用户: 恭喜你获得唻配设计师认证! ",
                    "text/html;charset=gb2312");
        } catch (MessagingException e) {
            e.printStackTrace();
            throw new DicmfException(SystemConstantes.ERROR, "设计师认证邮件,发送失败!");
        }
    }

    public void doSaveDesignCase(DesignCase record) {
        designCaseMapper.updateByPrimaryKeySelective(record);
    }

    @Override
    public PageList<Design> queryHouseModelByPage(String keyword, int page, int limit) {
        return customerMapper.queryHouseModelByPage(keyword, new PageBounds(page, limit));
    }

    public Customer get(Object id) {
        Customer c = customerMapper.selectByPrimaryKey(id);
        c.setCaselist(designCaseMapper.getUserDesignCase(Long.parseLong(id.toString())));
        return c;
    }

    public void doAddOrUpdate(Customer t) {
        String pwdHash = new Sha512Hash(t.getPassword()).toBase64();//之前的密码
        t.setPassword(pwdHash);
        int count = customerMapper.countCustomerByPhone(t.getPhone());
        if (count > 0) {
            throw new DicmfException(SystemConstantes.ERROR, "用户名已经存在");
        }
        customerMapper.insert(t);
    }

    public Customer doLogin(String username, String password, HttpServletRequest request) {
        String pwdHash = new Sha512Hash(password).toBase64();
        Customer customer = customerMapper.login(username, pwdHash);
        if (customer == null) {
            throw new DicmfException(SystemConstantes.ERROR, "登录失败,用户名或密码错误");
        }
        String token = MD5.GetMD5Code(username +"-" +new Date().getTime());
        customer.setLastLoginTime(new Date());
        customer.setToken(token);
        customerMapper.updateByPrimaryKeySelective(customer);
        request.getSession().setAttribute("login_user", customer);
        return customer;
    }

    public PageList<DesignCase> queryDesignPage(CustomerSearchCondition condition, int page, int limit) {
        return designCaseMapper.queryForPage(condition, new PageBounds(page, limit));
    }

    @Override
    public void doAuth(List<DesignCase> list) {
        Integer customerid = null;
        for (DesignCase designCase : list) {
            customerid = designCase.getUserid();
            designCaseMapper.insert(designCase);
        }
        Customer c = new Customer();
        c.setCustomerid(customerid);
        c.setStatus("2");
        customerMapper.updateByPrimaryKeySelective(c);
    }

    public List<Map<String, Object>> getUserDesign(Integer userid) {
        return customerMapper.getUserDesign(userid);
    }
   
	public void verifyToken(String token) {
		if("TEST_TOKEN".equals(token)) return ;
		if( ! (customerMapper.findEnableToken(token) > 0 )){
			throw new DicmfException(SystemConstantes.ERROR,"token失效请重新登录");
		} 
	}

}
